package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0098_IsValidBST
 * Description:
 * Author: xyf
 * Date: 9/7/22 10:33 PM
 * Version: 1.0
 **/
public class P0098_IsValidBST
{
    public boolean isValidBST(TreeNode root)
    {
        return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    private boolean dfs(TreeNode root, long min, long max)
    {
        if (root == null)
        {
            return true;
        }

        if (root.val <= min || root.val >= max)
        {
            return false;
        }

        return dfs(root.left, min, root.val) && dfs(root.right, root.val, max);
    }
}